package com.sale.plugins;

import com.apache.api.manager.ProxyManager;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.database.constant.SpringContextLoader;
import com.apache.exception.BusinessException;
import com.apache.ius.plugin.CustomMethodPlugin;
import com.apache.ius.plugin.IusPluginUtil;
import com.apache.tools.DataMap;
import com.apache.tools.StrUtil;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 挂卖单时判断有没有持仓：只有拥有持仓的用户才可以挂卖单
 */
public class SaleBuyForGoodsPluginImpl implements CustomMethodPlugin {

    private Logger log = Logger.getLogger(getClass());

    @SuppressWarnings("rawtypes")
    @Override
    public Object beforeInvoke(Map<String, Object> param) {
        log.info("总持仓参数补充，同时挂单时判断有没有持仓");
        long totalNum = 0l;
        String orderNum = String.valueOf(param.get("trsOrder.orderNum"));//本次委托数量
        if (StrUtil.isNull(orderNum)) {
            throw new BusinessException("委托数量不能为空！");
        }
        String orderFlag = String.valueOf(param.get("trsOrder.orderFlag"));
        if (Validator.isNotNull(orderFlag)) {
//            ProxyManager netProxyManager = (ProxyManager) SpringContextLoader
//                    .getBean("netProxyManager");
//            ParamsVo params = new ParamsVo();
//            params.setParams("doCode", "dymicSql");
//            params.setParams("sqlKeyId", "ByDymicSql");
//            params.setParams("resultType", "objInfo");
//            params.setKey("publicForSql");
//
//            //封装参数
//            params.setParams("modelTypes", "s_trsGoods");
//            ResultEntity dymicSqlParamResult = (ResultEntity) netProxyManager.doInvoke(params);
//            List<DataMap> list = (List) dymicSqlParamResult.getEntity();


            Map<String, Object> whereMap = new HashMap<String,Object>();
            //封装参数
            whereMap.put("trsGoods.w_proNo", param.get("trsOrder.proNo"));
            whereMap.put("trsGoods.w_userId", param.get("trsOrder.userId"));

            ResultEntity resultEntity1 = IusPluginUtil.getInstance().select("list","s_trsGoods",whereMap);
            if(resultEntity1==null || resultEntity1.getEntity()==null){
                throw new BusinessException("查询持仓失败！");
            }
            List<DataMap> list = (ArrayList<DataMap>)resultEntity1.getEntity();

            if (list != null && list.size() > 0) {

                for (DataMap dataMap : list) {
                    long useNum = Math.round(Double.parseDouble(Validator.getDefaultStr(String.valueOf(dataMap.get("useNum")), "0")));
                    totalNum = Math.round(Double.parseDouble(Validator.getDefaultStr(String.valueOf(dataMap.get("totalNum")), "0")));
                    long orderNumLong = Long.valueOf(orderNum);
                    if ("sell".equals(orderFlag) && (useNum - orderNumLong) < 0) {
                        throw new BusinessException(
                                "【" + param.get("trsOrder.proNo") + "】可用数量不足，不允许卖空！");
                    }
                }
            } else {
                if ("sell".equals(orderFlag)) {
                    throw new BusinessException(
                            "【" + param.get("trsOrder.proNo") + "】可用数量不足，不允许卖空！");
                }else{
                    log.info("挂买单不判断是否有持仓！");
                }
            }
        }
        param.put("totalNum", totalNum);

        return null;
    }

    @Override
    public Object doInvoke(Map<String, Object> param) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Object afterInvoke(Map<String, Object> param) {

        // TODO Auto-generated method stub
        return null;
    }

    public void run(ParamsVo paramsVo) {
        beforeInvoke(paramsVo.getParams());
    }

}
